<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/kinder.php";
$kind = new Kinder();
if(isset($_REQUEST['id']) && $_REQUEST['id']!="") {
	$kind = new Kinder($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
	$kind->setAdresseId($_REQUEST['adresse_id']);
    $kind->setAdresseShow(isset($_REQUEST['adresse_show'])?1:0);
	$kind->setMutterId($_REQUEST['mutter_id']);
    $kind->setMutterSorge(isset($_REQUEST['mutter_sorge'])?1:0);
	$kind->setVaterId($_REQUEST['vater_id']);
    $kind->setVaterSorge(isset($_REQUEST['vater_sorge'])?1:0);
	$kind->setNachname($_REQUEST['nachname']);
	$kind->setVorname($_REQUEST['vorname']);
    $kind->setNameShow(isset($_REQUEST['name_show'])?1:0);
	$kind->setGeburtsdatum($_REQUEST['geburtsdatum']);
	$kind->setMigration($_REQUEST['migration']?1:0);
	$kind->setZurueckgestellt($_REQUEST['zurueckgestellt']);
    $kind->setBilderDruck(isset($_REQUEST['bilder_druck'])?1:0);
    $kind->setBilderOnline(isset($_REQUEST['bilder_online'])?1:0);
	$kind->save();
}
$vater = $kind->getVater();
$mutter = $kind->getMutter();
$adress = $kind->getAdresse();
?>
<script language="JavaScript" type="text/javascript">
    $(function() {
        $("input[name=adresse]").autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_adress',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	var item = ui.item;
            	$('input[name=adresse_id]').attr('value', item.id);
            	$('#stammdaten_kinder_add_adress').html(item.strasse+" "+item.hausnummer+item.zusatz_hausnummer+"<br />"+item.plz+" "+item.ort)
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.vorname + " " + item.nachname + "<br />"
        			+ item.strasse + " " + item.hausnummer + item.zusatz_hausnummer + "<br />"
        			+ item.plz + " " + item.ort + "</a>";
        	item.value = item.vorname + " " + item.nachname;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
        $("input[name=mutter]").autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_adress',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	$('input[name=mutter_id]').attr('value', ui.item.id);
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.vorname + " " + item.nachname + "</a>";
        	item.value = item.vorname + " " + item.nachname;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
        $("input[name=vater]").autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_adress',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	$('input[name=vater_id]').attr('value', ui.item.id);
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.vorname + " " + item.nachname + "</a>";
        	item.value = item.vorname + " " + item.nachname;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
        $('input[name="geburtsdatum"]').datepicker({
        	'dateFormat': 'yy-mm-dd'
        });
    });
</script>
<?php
if($action=='edit') {
	$action = 'add';
}
if($action=='add') {
	$form_display = "block";
} else {
	$form_display = "none";
}
?>
<form id='stammdaten_kinder_add_form' method="post" action="/index.php" style='display:<?php echo $form_display; ?>'>
	<input type="hidden" name="page" value="<?php echo $menuid;?>" />
	<input type="hidden" name="id" value="<?php echo $kind->getId(); ?>" />
    <div>
    	<div class='inline-block label'>Vorname:</div>
    	<div class='inline-block'>
	        <input type="text" name="vorname" maxlength="50" value="<?php echo $kind->getVorname(); ?>" /><br />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Nachname:</div>
    	<div class='inline-block'>
	        <input type="text" name="nachname" maxlength="50" value="<?php echo $kind->getNachname(); ?>" /><br />
    	</div>
    </div>
    <div>
        <div class='inline-block label'>Name Ver&ouml;ffentlichen:</div>
        <div class='inline-block'>
            <input name="name_show" type="checkbox" value="true" <?php echo $kind->isNameShow()?"checked='true'":""; ?> />
        </div>
    </div>
    <div>
    	<div class='inline-block label'>Geburtstag:</div>
    	<div class='inline-block'>
	        <input type="text" name="geburtsdatum" maxlength="10" value="<?php echo $kind->getGeburtsdatum(); ?>" /><br />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Adresse:</div>
    	<div class='inline-block'>
	        <input name="adresse_id" type="hidden" value="<?php echo $kind->getAdresseId(); ?>" />
	        <input type="text" name="adresse" maxlength="101" value="<?php echo ($adress->getVorname()!=""?$adress->getVorname()." ":"").$adress->getNachname();?>" /><br />
	        <span id="stammdaten_kinder_add_adress">
	        	<?php echo $adress->getStrasse()." ".$adress->getHausnummer().$adress->getZusatzHausnummer(); ?><br />
	        	<?php echo $adress->getPlz()." ".$adress->getOrt(); ?>
	        </span>
    	</div>
    </div>
    <div>
        <div class='inline-block label'>Adresse Ver&ouml;ffentlichen:</div>
        <div class='inline-block'>
            <input name="adresse_show" type="checkbox" value="true" <?php echo $kind->isAdresseShow()?"checked='true'":""; ?> />
        </div>
    </div>
    <div>
    	<div class='inline-block label'>Mutter:</div>
    	<div class='inline-block'>
	        <input name="mutter_id" type="hidden" value="<?php echo $kind->getMutterId(); ?>" />
	        <input type="text" name="mutter" maxlength="101" value="<?php echo ($mutter->getVorname()!=""?$mutter->getVorname()." ":"").$mutter->getNachname();?>" />
    	</div>
    </div>
    <div>
        <div class='inline-block label'>Sorgeberechtigt:</div>
        <div class='inline-block'>
            <input name="mutter_sorge" type="checkbox" value="true" <?php echo $kind->isMutterSorge()?"checked='true'":""; ?> />
        </div>
    </div>
    <div>
    	<div class='inline-block label'>Vater:</div>
    	<div class='inline-block'>
	        <input name="vater_id" type="hidden" value="<?php echo $kind->getVaterId(); ?>" />
	        <input type="text" name="vater" maxlength="101" value="<?php echo ($vater->getVorname()!=""?$vater->getVorname()." ":"").$vater->getNachname();?>" />
    	</div>
    </div>
    <div>
        <div class='inline-block label'>Sorgeberechtigt:</div>
        <div class='inline-block'>
            <input name="vater_sorge" type="checkbox" value="true" <?php echo $kind->isVaterSorge()?"checked='true'":""; ?> />
        </div>
    </div>
    <div>
    	<div class='inline-block label'>Migrationshintergrund:</div>
    	<div class='inline-block'>
	        <input name="migration" type="checkbox" value="true" <?php echo $kind->isMigration()?"checked='true'":""; ?> />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Zur&uuml;ckgestellt:</div>
    	<div class='inline-block'>
	        <input name="zurueckgestellt" type="text" maxlength="100" value="<?php echo $kind->getZurueckgestellt(); ?>" />
    	</div>
    </div>
    <div>
        <div class='inline-block label'>Erlaubnis f&uuml;r Bilder Druck:</div>
        <div class='inline-block'>
            <input name="bilder_druck" type="checkbox" value="true" <?php echo $kind->isBilderDruck()?"checked='true'":""; ?>" />
        </div>
    </div>
    <div>
        <div class='inline-block label'>Erlaubnis f&uuml;r Bilder Online:</div>
        <div class='inline-block'>
            <input name="bilder_online" type="checkbox" value="true" <?php echo $kind->isBilderOnline()?"checked='true'":""; ?>" />
        </div>
    </div>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
<?php
if($action!="add") {
	$kinder = Kinder::getAll();
?>
<div>
    <a href='?page=stammdaten_kinder&action=add'>Neues Kind</a>
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
        	<th></th>
            <th>Name</th>
            <th>Geburtstag</th>
            <th>Mutter</th>
            <th>Vater</th>
            <th>Migration</th>
            <th>Zur&uuml;ckgestellt</th>
        </tr>
<?php
if($kinder!=NULL) {
	for($i=0;$i<count($kinder);$i++) {
		$value = $kinder[$i];
		$adress = $value->getAdresse();
		$vater = $value->getVater();
		$mutter = $value->getMutter();
		$css_class = "even";
		if($i%2==1) {
			$css_class = "odd";
		}
?>
		<tr class="<?php echo $css_class; ?>">
			<td><?php echo ($i+1); ?></td>
			<td><?php echo $value->getVorname()." ".$value->getNachname();?></td>
			<td><?php echo $value->getGeburtsdatum();?></td>
			<td><?php echo $mutter->getVorname()." ".$mutter->getNachname();?></td>
			<td><?php echo $vater->getVorname()." ".$vater->getNachname();?></td>
			<td><?php echo $value->isMigration()?"X":""; ?></td>
			<td><?php echo $value->isZurueckgestellt()?"X":""; ?></td>
			<td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $value->getId();?>">edit</a></td>
		</tr>
<?php
	}
}
?>
    </table>
</div>
<?php    
}
?>